package com.owen.server.impl;

import com.owen.dao.SupplierDao;
import com.owen.pojo.Supplier;
import com.owen.server.SupplierServer;
import com.owen.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierServerImpl implements SupplierServer {

    @Autowired
    private SupplierDao supplierDao;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<Supplier> findAll() {
        List<Supplier> suppliers = (List<Supplier>)redisUtil.get("findSupplier");
        if(suppliers == null){
            suppliers = supplierDao.findAll();
            redisUtil.set("findSupplier", suppliers);
        }
        return suppliers;
    }

    @Override
    public List<Supplier> findByName(Supplier supplier) {
        return supplierDao.findByName(supplier);
    }

    @Override
    public Supplier findById(Integer id) {
        return supplierDao.findById(id);
    }

    @Override
    public Integer addSupplier(Supplier supplier) {
        Integer integer = supplierDao.addSupplier(supplier);
        redisUtil.del("findSupplier");
        return integer;
    }

    @Override
    public Integer deleteById(Integer id) {
        Integer integer = supplierDao.deleteById(id);
        redisUtil.del("findSupplier");
        return integer;
    }

    @Override
    public Integer updateById(Supplier supplier) {
        Integer integer = supplierDao.updateById(supplier);
        redisUtil.del("findSupplier");
        return integer;
    }

    @Override
    public Integer deleteBatch(Integer[] ids) {
        Integer integer = supplierDao.deleteBatch(ids);
        redisUtil.del("findSupplier");
        return integer;
    }

}
